<?php

namespace app\controller\system;

use app\model\system\SysDict;
use app\model\system\SysDictData;
use app\controller\BaseController;
use app\model\system\SysDictItem;
use support\Request;
use support\Response;

class DictController extends BaseController
{

    public function create(Request $request): Response
    {
        $data = $request->post();
        $user = SysDict::create($data);
        if (!$user) {
            return $this->error('创建失败');
        } else {
            return $this->success([]);
        }
    }

    public function form(int $id): Response
    {
        $log = SysDict::find($id);
        if (!$log) {
            return $this->error('用户不存在');
        }
        return $this->success($log->getFormArray());
    }


    public function update(Request $request, int $id): Response
    {
        $data = $request->post();
        $user = SysDict::find($id);

        if (!$user) {
            return $this->error('用户不存在');
        }

        if ($user->update($data)) {
            return $this->success($user->getFormArray());
        } else {
            return $this->error('更新失败');
        }
    }

    public function delete(int $id): Response
    {
        $user = SysDict::find($id);
        if (!$user) {
            return $this->error('用户不存在');
        }
        if (!$user->delete()) {
            return $this->error('删除失败');
        } else {
            return $this->success($user->toArray());
        }
    }

    public function page(Request $request): Response
    {
        $pageSize = $request->get("pageSize", 10);
        $pageNum = $request->get("pageNum", 1);
        $result = SysDict::order("id", "desc")->paginate(['list_rows' => $pageSize, 'page' => $pageNum]);
        return $this->success(["list" => $result->items(), "total" => $result->total()]);
    }

    public function getItems(Request $request, string $dictCode): Response
    {
        $params = $request->get();
        extract($params);
        $dicts = SysDictItem::where("dict_code", $dictCode)->where("status", 1)->column(["value", "label", "tag_type" => "tagType"]);
        return $this->success($dicts);
    }

    public function getItemsPage(Request $request, string $dictCode): Response
    {
        $params = $request->get();
        extract($params);
        $pageData = SysDictItem::where("dict_code", $dictCode)->paginate(["list_rows" => $pageSize, "page" => $pageNum]);
        return $this->success(["list" => $pageData->items(), "total" => $pageData->total()]);
    }
    public function getItemOptions(string $dictCode): Response
    {
        $list = SysDictItem::where("dict_code", $dictCode)->column(["value", "label", "tag_type" => "tagType"]);
        return $this->success($list);
    }

    public function itemForm(string $dictCode, int $itemId): Response
    {
        $item = SysDictItem::find($itemId);
        if (!$item) {
            return $this->error('数据不存在');
        }
        return $this->success($item->getFormArray());
    }

    public function updateItem(Request $request, string $dictCode, int $itemId): Response
    {
        $data = $request->post();
        $item = SysDictItem::find($itemId);
        if (!$item) {
            return $this->error('数据不存在');
        }
        if ($item->update($data)) {
            return $this->success($item->getFormArray());
        } else {
            return $this->error('更新失败');
        }
    }

    public function setItems(Request $request, string $dictCode): Response
    {
        $data = $request->post();
        $data["dict_code"] = $dictCode;
        $dictItem = SysDictItem::create($data);
        if (!$dictItem) {
            return $this->error('创建失败');
        } else {
            return $this->success([]);
        }
    }

    public function deleteItems(string $dictCode, string $itemIds): Response
    {
        $ids = explode(",", $itemIds);
        $dictItems = SysDictItem::destroy($ids);
        if (!$dictItems) {
            return $this->error('删除失败');
        } else {
            return $this->success(["ids" => $ids]);
        }
    }
}
